package com.sds.prolog.model;

import com.google.common.collect.Sets;
import com.sds.system.models.IdentifiedObject;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Set;

@XmlRootElement
@Entity
public class Rule implements IdentifiedObject {
    private String id;
    private Set<String> material = Sets.newHashSet();
    private Set<String> produces = Sets.newHashSet();

    public Rule() {
        material = Sets.newHashSet();
        produces = Sets.newHashSet();
    }

    @Id
    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<String> getMaterial() {
        return material;
    }

    public void setMaterial(Set<String> material) {
        this.material = material;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<String> getProduces() {
        return produces;
    }

    public void setProduces(Set<String> produces) {
        this.produces = produces;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        Rule rule = (Rule) o;

        if (material != null ? !material.equals(rule.material) : rule.material != null) return false;
        return !(produces != null ? !produces.equals(rule.produces) : rule.produces != null);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (material != null ? material.hashCode() : 0);
        result = 31 * result + (produces != null ? produces.hashCode() : 0);
        return result;
    }
}
